//options
clear all
set maxvar  32000, permanently
set matsize 11000, permanently
set more off, permanently
global bootstraps 1000
set seed 23

// macros
global klmshare				: env klmshare
global projects				: env projects
global klmperry             : env klmperry
global storageb				: env storageb

global perrydatas       = "${klmperry}/CBA/data/perry/raw"
global perrydata        = "${klmperry}/CBA/data/perry/clean"
global masterinter  	= "${klmshare}/CurrentRAs/FredB/test"
global output           = "${projects}/ece_parenting/tex_files/other"
global dataperry        = "$klmperry/PerryPreschool/Data/Perry_PARI_and_Other_Data/PARI/DATA_PARI/"
global abcjpeanalysis   = "${klmshare}/Data_Central/Abecedarian/data/ABC-CARE/extensions/cba-iv/"
global dataihdp         = "${projects}/ece_parenting/data"
global dataihdp         = "$storageb/dc_data/"

cd  $dataihdp
use ihdp_data, clear
rename _all, lower

replace   bw = bw/1000
destring bwg , replace 

// impute iq
replace   stndscor =  iqcage     if stndscor ==.
replace   iqcage   =  stndscor   if iqcage ==.
foreach var of varlist stndscor iqcage { 
	replace `var' = mdi_24cor    if stndscor ==. & iqcage ==.
}
replace   stndscor =  iqcage     if stndscor ==.
replace   iqcage   =  stndscor   if iqcage ==.

// list relevant variables
global baseline_child      twin sex bw anga black hispanic 
global baseline_mother     mage meduc works married 
global baseline_household  welfare tot_siblings_natural employed_adult
global baseline_economy    employment medinc gpc
global outputs             iqcage stndscor 
global inputs              cum_avg_daycare_36m_sum
global parlater            actvty5 literacy5 books5 tvsage5 diction5 outings5 control author democrat yellage8 cohes8 conf8 cohes18 conf18 yell4wks supervc matschl educasp

// mark all sample
reg $baseline_child
gen sample0 = e(sample)

// mark baseline sample
reg $baseline_child $baseline_mother $baseline_household $baseline_economy $outputs $inputs
gen sample1 = e(sample)

// parenting latents, ages 1 and 3
foreach var of varlist alt_subscale_1_12-alt_subscale_6_12 alt_subscale_1_36-alt_subscale_8_36 {
	summ `var'																									   if sample1 == 1
	gen  `var'_std = (`var' - r(mean))/r(sd)                                                                       if sample1 == 1
}
# delimit
sem (_cons@0 X -> alt_subscale_1_12_std) (_cons@0 X -> alt_subscale_2_12_std) (_cons@0 X -> alt_subscale_3_12_std) 
	(_cons@0 X -> alt_subscale_4_12_std) (_cons@0 X -> alt_subscale_5_12_std) (_cons@0 X -> alt_subscale_6_12_std) if sample1 == 1, method(adf);
predict parenting_age1 if sample1 == 1, latent;
sem (_cons@0 X -> alt_subscale_1_36_std) (_cons@0 X -> alt_subscale_2_36_std) (_cons@0 X -> alt_subscale_3_36_std) (_cons@0 X -> alt_subscale_4_36_std)
	(_cons@0 X -> alt_subscale_5_36_std) (_cons@0 X -> alt_subscale_6_36_std) (_cons@0 X -> alt_subscale_7_36_std) (_cons@0 X -> alt_subscale_8_36_std) if sample1 == 1, 
																																						 cov(e.alt_subscale_1_36_std*e.alt_subscale_2_36_std)
																																						 cov(e.alt_subscale_3_36_std*e.alt_subscale_4_36_std)
																																						 cov(e.alt_subscale_5_36_std*e.alt_subscale_6_36_std)
																																						 cov(e.alt_subscale_7_36_std*e.alt_subscale_8_36_std)
																																						 cov(e.alt_subscale_4_36_std*e.alt_subscale_7_36_std)
																																						 cov(e.alt_subscale_2_36_std*e.alt_subscale_7_36_std) method(adf);		
predict parenting_age3 if sample1 == 1, latent;
# delimit cr
egen    parenting_ages13 = rowmean(parenting_age1 parenting_age3) if sample1 == 1

global inputs_std   cum_avg_daycare_36m_sum parenting_ages13
keep   ihdp site tg bwg sample* $baseline_child $baseline_mother $baseline_household $baseline_economy $inputs_std $outputs $parlater alt_subscale_1_12-alt_subscale_6_12 alt_subscale_1_36-alt_subscale_8_36 ppvtstd wasifsiq

// merge in ages 5 and 8
merge 1:1 ihdp using iqchildhoodihdp
keep  if _merge == 3
drop _merge

// impute iq
replace kidppvt5 = wippsif5 if kidppvt5 ==.
replace wippsif5 = kidppvt5 if wippsif5 ==.

replace ppvtstd8 = fsiq8    if ppvtstd8 ==.
replace fsiq8    = ppvtstd8 if fsiq8    ==.

replace ppvtstd  = wasifsiq if ppvtstd  ==.
replace wasifsiq = ppvtstd  if wasifsiq ==.

reg fsiq8 ppvtstd8 kidppvt5 wippsif5 wasifsiq ppvtstd
gen sample2 = e(sample) 

// outcomes
merge 1:1 ihdp using adultihdp
keep if _merge == 3
drop    _merge

// construct "positive" outcomes
replace idle18y         = 1 - idle18y
replace sch_eversped18y = 1 - sch_eversped18y
replace sch_math18y     = 1 - sch_math18y
replace sch_read18y     = 1 - sch_read18y
replace sch_thrp18y     = 1 - sch_thrp18y
replace teen18y         = 1 - teen18y

gen     smoke18 = .
replace smoke18 = 1 if cigs18y >  0  & cigs18y != .
replace smoke18 = 0 if cigs18y == 0 & cigs18y != .
replace smoke18 = 1 - smoke18

gen     absence18 = .
replace absence18 = 0 if sch_dabs18y < 5  & sch_dabs18y != .
replace absence18 = 1 if sch_dabs18y >= 5 & sch_dabs18y != .
replace absence18 = 1 - absence18 

// marking missing
global ed18  sch_eversped18y sch_math18y sch_read18y sch_test18y
global beh18 smoke18 idle18y sch_thrp18y teen18y
egen missing     = rowmiss($ed18 $beh18)

// average
egen avg_outcome_educ18 = rowmean($ed18)        if missing == 0
egen avg_outcome_beh18  = rowmean($beh18)       if missing == 0
egen avg_outcome18      = rowmean(avg_outcome_educ18 avg_outcome_beh18) if missing == 0

// define sample
reg     avg_outcome18 if sample2 == 1
replace sample2 = 0   if e(sample) == 0
replace sample2 = 0   if   sample1 == 0

// standardize by sample
foreach num of numlist 0(1)2 {
	gen p`num' = parenting_ages13                  if sample`num' == 1
	summ     p`num' if tg == 0                      & sample`num' == 1
	replace  p`num' = (p`num' - r(mean))/r(sd)     if sample`num' == 1
	replace  p`num' = p`num' + 100                 if sample`num' == 1
	gen      c`num' = cum_avg_daycare_36m_sum      if sample`num' == 1
	summ     c`num'  if tg == 0                     & sample`num' == 1
	replace  c`num'  = (c`num'    - r(mean))/r(sd) if sample`num' == 1
	replace  c`num'  =  c`num'    + r(mean)        if sample`num' == 1
}
keep if sample2 == 1

// construc later-dat parenting
global par5     actvty5 literacy5 books5 outings5
global par8     cohes8 conf8 yellage8
global par58    actvty5 literacy5 books5 outings5 cohes8 conf8 yellage8
global par18    cohes18 conf18 yell4wks
global par5818  cohes8 conf8 yellage8 cohes18 conf18 yell4wks

// standardize and list factor variables
foreach num of numlist 5 8 58 18 5818 {
	global semvars_`num'
	foreach var of varlist ${par`num'} {
		summ    `var'
		replace `var' = (`var' - r(mean))/r(sd)
		global semvars_`num' ${semvars_`num'} (_cons@0 X -> `var')
	}
}
drop p0-c1 c2 parenting*
rename p2 parenting_age13

// construct parenting measures
sem ${semvars_18}
predict parenting_age18, latent

foreach num in 18 {
	summ    parenting_age`num' if tg == 0
	replace parenting_age`num' = ((parenting_age`num' - r(mean))/r(sd)) + 100

	rename  parenting_age`num' parlate`num' 
	summ    parlate`num', d
	foreach dec in 25 50 75 {
		gen     parlate`num'`dec'ind  = 0  if parlate`num'  !=.
		replace parlate`num'`dec'ind  = 1  if parlate`num' > r(p`dec') & parlate`num' !=.
	}
}

// treatments effects
global parvars parlate18 parlate1875ind 

// describe by sample
global sample_singletons   if twin == 0
global sample_twins        if twin == 1

matrix all = J(1,8,.)
foreach var of varlist $parvars {
	foreach group in singletons twins {
		bootstrap, strata(bwg site tg) reps($bootstraps) : reg `var' tg ${sample_`group'}
		matrix  c`var'_g`group' = e(b)[1,2]
		matrix  b`var'_g`group' = e(b)[1,1]
		matrix se`var'_g`group'      = sqrt(e(V)[1,1])
		matrix  t`var'_g`group'      = abs(b`var'_g`group'[1,1]/se`var'_g`group'[1,1])
		matrix df`var'_g`group' = e(N) - e(rank)
		matrix  p`var'_g`group' = (1 - normal(t`var'_g`group'[1,1]))
		
		matrix `var'_`group' = [c`var'_g`group',b`var'_g`group',p`var'_g`group'] 
	}
	matrix `var' = [`var'_singletons,`var'_twins]
}
// twin-singleton differences
gen singleton = 1 - twin
foreach var of varlist $parvars {
	gen  `var'_hip1 = `var'
	foreach sib in 0 1 {
		summ    `var'_hip1                        if tg == 0 & twin == `sib'
		replace `var'_hip1 = `var'_hip1 - r(mean) if twin == `sib'
	}
	bootstrap, strata(bwg site tg) reps($bootstraps) : reg `var'_hip1 singleton if tg == 1
	matrix  b`var'_hip1 = e(b)[1,1]
	matrix se`var'_hip1 = sqrt(e(V)[1,1])
	matrix  t`var'_hip1 = abs(b`var'_hip1[1,1]/se`var'_hip1[1,1])
	matrix df`var'_hip1 = e(N) - e(rank)
	matrix  p`var'_hip1 = (1 - normal(t`var'_hip1[1,1]))
	
	matrix `var'_hip1 = [b`var'_hip1,p`var'_hip1]
	
	matrix `var' = [`var',`var'_hip1]
	matrix all = [all \ `var']
}
matrix all = all[2...,1...]

// table
matrix all = [ J(1,8,.)  \ all[1..2,1...]]
clear
svmat all
gen   n = _n

// format
foreach var of varlist all* {
	gen    `var'and = "&" if n !=1
}

// header
gen all0and = "&" if n != 1

gen     all1_0   = "  \multicolumn{9}{l}{\textbf{\textit{Panel b}. Age Eighteen}}"		                         if n == 1
replace all0and  = "   "           						      		                		   					 if n == 1
replace all1and  = "   "           						      		                		   					 if n == 1
replace all2and  = "   "           						      		                		   					 if n == 1
replace all3and  = "   "           						      		               			   					 if n == 1
replace all4and  = "   "           						      		               			   					 if n == 1
replace all5and  = "   "           						      		               			   					 if n == 1
replace all6and  = "   "           						      		                		   					 if n == 1
replace all7and  = "   "           						      		               			   					 if n == 1


replace all1_0  = "   \multicolumn{1}{l}{\hspace{4mm} Parenting}" 		 		       					   if n == 2  
replace all1_0  = "   \multicolumn{1}{l}{\hspace{4mm} Parenting $ > 75 $ Percentile}" 		 	           if n == 3 
// order
global orderlist all1_0 all0and
foreach num of numlist 1(1)8 {
	global orderlist $orderlist all`num' all`num'and
}
order $orderlist

// other formating
replace all8and = "\\   "
replace all8and = "\\ \bottomrule \end{tabular}"           if n == 3

// put together in string
gen all = " "
foreach num of numlist 1(1)8 {
	tostring all`num'  , replace force format(%15.2fc)
	replace  all`num'  = "" if all`num'  == "."
}

global orderin all1_0 all0and
foreach num of numlist 1(1)8 {
	global orderin $orderin all`num' all`num'and 
}
global orderin $orderin all`num'
order $orderin n

foreach var of varlist all1_0-all8and {	
	replace  all = all + `var'
	replace  all`num' = " " if all`num' == "."
}

// save in tex format
keep all
cd $output
outsheet using laterparentingdescribe.tex, noquote nonames replace
